package com.hhkj.api.order.controller;

import com.hhkj.api.order.model.req.*;
import com.hhkj.api.order.service.biz.ApiOrderBizService;
import com.hhkj.api.sale.common.session.model.LoginSale;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.login.api.ApiLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ApiOrderController
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/1 21:54
 */
@RequestMapping("/api/sale/order")
@RestController
public class ApiOrderController extends BaseController {

    @Autowired
    private ApiOrderBizService apiOrderBizService;

    /** 线下支付 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/offline/pay")
    public AjaxResult payByOffline(@Validated @RequestBody ApiOrderOfflinePayReq req) {
        return AjaxResult.success(apiOrderBizService.payByOffline(req));
    }

    /** 创建微信支付 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/wechat/pay")
    public AjaxResult payByWechat(@Validated @RequestBody ApiOrderWechatPayReq req) {
        return AjaxResult.success(apiOrderBizService.payByWechat(req));
    }

    /** 统计 */
    @ApiLogin(LoginSale.class)
    @GetMapping("/count")
    public AjaxResult count() {
        return AjaxResult.success(apiOrderBizService.count());
    }

    /** 取消 */
    @ApiLogin(LoginSale.class)
    @PutMapping("/cancel")
    public AjaxResult cancel(@Validated @RequestBody ApiOrderCancelReq req) {
        return apiOrderBizService.cancel(req) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 分页 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/include/item/paging")
    public TableDataInfo pagingIncludeItem(@RequestBody ApiOrderIncludeItemReq req) {
        startPageBody(req);
        return getDataTable(apiOrderBizService.pagingIncludeItem(req));
    }

    /** 获取订单详情 */
    @ApiLogin(LoginSale.class)
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(apiOrderBizService.getDetailById(id));
    }

    /** 分页 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/paging")
    public TableDataInfo paging(@RequestBody ApiOrderReq req) {
        startPageBody(req);
        return getDataTable(apiOrderBizService.paging(req));
    }

    /** 提交订单 */
    @ApiLogin(LoginSale.class)
    @PostMapping("")
    public AjaxResult save(@RequestBody ApiOrderSaveReq apiOrderReq) {
        return AjaxResult.success(apiOrderBizService.save(apiOrderReq));
    }
}
